<!-- 子女信息 -->
<template>
  <zh-form ref="formRef" :transprent="true" @submit="submit" v-model="form" inputAlign="right" :label-width="200" :margin="[0, 24]" :padding="[10, 0]">
    <zh-card :width="620">
      <template v-slot:title>
        <view style="width: 100%">
          <zh-button
            v-if="props.myCheckInfo.bmFlag && form.authStatus === '1' && form.canEnrollFlag"
            :color="form.bmFlag ? 'green' : 'primary'"
            :label="form.bmFlag ? '已报名，前往我的报名查看' : '一键报名'"
            block
            @click="gotoYjbm"
          ></zh-button>
          <zh-sheet v-if="props.myCheckInfo.bmFlag && form.authStatus === '1' && !form.canEnrollFlag" :round="5" :margin="[0,0]" :padding="[20,20]"  text color="primary">
            <zh-text class="text-weight-b" label="温馨提示:"></zh-text>
            <zh-text style="text-indent: 2em;padding-top:10rpx" >您目前没有取得<span style="color: red;">东阳市公安局核发的处于有效期内的《浙江省居住证》</span>，不符合东阳义务段学校新生报名最低条件</zh-text>
            <zh-text style="text-indent: 2em;padding-top:10rpx"  color="red"  label="若信息有误，请致电相关学校。"></zh-text>
          </zh-sheet>
          <view class="flex-row-center-between">
            <view style="border-left: 10rpx solid #2f54eb" class="mt-10">
              <zh-text :fontSize="36" class="text-weight-b pl-10" :label="form.fullName + '基本信息'"></zh-text>
            </view>
            <tm-icon v-if="data.disabled" :fontSize="42" :name="data.hiddenFlag ? 'tmicon-eye-slash' : 'tmicon-eye'" style="margin: 0 20rpx" @tap="changeSensitive"></tm-icon>
          </view>
        </view>
      </template>

      <template v-slot:content>
        <zh-alert
          v-if="form.authStatus === '1' && !form.canEnrollFlag&&props.myCheckInfo.hjCheck.showBaseFlag"
          :margin="[0, 10]"
          :width="620"
          :closable="false"
          color="red"
          linear="right"
          :content="content"
          :height="120"
          :maxLine="3"
        ></zh-alert>
        <view v-if="form.expandFlag">
          <zh-form-select
            :disabled="data.disabled"
            field="areaCode"
            v-if="data.areaCodeDict.length > 1"
            :rules="[{ required: true, message: '请选择想要就读区县' }]"
            placeholder="请选择想要就读区县"
            v-model="form.areaCode"
            :options="data.areaCodeDict"
            label="就读区县"
            required
            :transprent="true"
          ></zh-form-select>
          <zh-form-input
            :disabled="data.disabled"
            field="fullName"
            :rules="[{ required: true, message: '请输入学生姓名' }]"
            placeholder="请输入学生姓名"
            v-model="form.fullName"
            label="学生姓名"
            required
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-select
            :disabled="true"
            :showClear="false"
            field="cardType"
            :rules="[{ required: true, message: '请选择学生证件类型' }]"
            placeholder="请选择学生证件类型"
            v-model="form.cardType"
            :options="data.cardTypeDict"
            label="学生证件类型"
            required
            :transprent="true"
          ></zh-form-select>
          <zh-form-input
            :disabled="true"
            field="idcard"
            :type="form.cardType === '01' ? 'idcard' : 'text'"
            :rules="[{ required: true, message: '请输入学生证件号码' }]"
            placeholder="请输入学生证件号码"
            v-model="form.idcard"
            label="学生证件号码"
            required
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-datetime
            :disabled="data.disabled"
            v-if="form.cardType !== '01'"
            field="birthYmd"
            :rules="[{ required: true, message: '请输入学生出生日期' }]"
            placeholder="请输入学生出生日期"
            v-model="form.birthYmd"
            label="出生日期"
            required
            showClear
            model="day"
            :transprent="true"
          ></zh-form-datetime>
          <zh-form-select
            :disabled="data.disabled"
            v-if="form.cardType !== '01'"
            field="sex"
            :rules="[{ required: true, message: '请选择学生性别' }]"
            placeholder="请选择学生性别"
            v-model="form.sex"
            :options="data.sexDict"
            label="性别"
            required
            showClear
            :transprent="true"
          ></zh-form-select>
          <zh-form-input
            v-if="props.myCheckInfo.showOtherInfo"
            :disabled="data.disabled"
            field="address"
            :rules="[{ required: true, message: '请输入学生现居住地' }]"
            placeholder="请输入学生现居住地"
            v-model="form.address"
            label="现居住地"
            required
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-input
            v-if="props.myCheckInfo.showOtherInfo"
            :disabled="data.disabled"
            field="householdPlace"
            :rules="[{ required: true, message: '请输入学生户籍地' }]"
            placeholder="请输入学生户籍地"
            v-model="form.householdPlace"
            label="户籍地"
            required
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-input
            :disabled="data.disabled"
            field="fatherName"
            :rules="[{ required: false, message: '请输入父亲(监护人)姓名' }]"
            placeholder="请输入父亲(监护人)姓名"
            v-model="form.fatherName"
            label="父亲(监护人)姓名"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-select
            :disabled="data.disabled"
            :showClear="false"
            field="fatherCardType"
            :rules="[{ required: false, message: '请输入父亲(监护人)证件类型' }]"
            placeholder="请输入父亲(监护人)证件类型"
            v-model="form.fatherCardType"
            :options="data.cardTypeDict"
            label="父亲(监护人)证件类型"
            :transprent="true"
          ></zh-form-select>
          <zh-form-input
            :disabled="data.disabled"
            field="fatherIdcard"
            :type="form.fatherCardType === '01' ? 'idcard' : 'text'"
            :rules="[{ required: false, message: '请输入父亲(监护人)证件号码' }]"
            placeholder="请输入父亲(监护人)证件号码"
            v-model="form.fatherIdcard"
            label="父亲(监护人)身份证"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-input
            v-if="props.myCheckInfo.showOtherInfo"
            :disabled="data.disabled"
            field="fatherCellphone"
            :rules="[{ required: false, message: '请输入父亲(监护人)手机' }]"
            number
            placeholder="请输入父亲(监护人)手机"
            v-model="form.fatherCellphone"
            label="父亲(监护人)手机"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-input
            :disabled="data.disabled"
            field="motherName"
            :rules="[{ required: false, message: '请输入母亲姓名' }]"
            placeholder="请输入母亲姓名"
            v-model="form.motherName"
            label="母亲姓名"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-select
            :disabled="data.disabled"
            :showClear="false"
            field="motherCardType"
            :rules="[{ required: false, message: '请输入母亲证件类型' }]"
            placeholder="请输入母亲证件类型"
            v-model="form.motherCardType"
            :options="data.cardTypeDict"
            label="母亲证件类型"
            :transprent="true"
          ></zh-form-select>
          <zh-form-input
            :disabled="data.disabled"
            field="motherIdcard"
            :type="form.motherCardType === '01' ? 'idcard' : 'text'"
            :rules="[{ required: false, message: '请输入母亲证件号码' }]"
            placeholder="请输入母亲证件号码"
            v-model="form.motherIdcard"
            label="母亲证件号码"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-input
            v-if="props.myCheckInfo.showOtherInfo"
            :disabled="data.disabled"
            field="motherCellphone"
            :rules="[{ required: false, message: '请输入母亲手机' }]"
            type="number"
            placeholder="请输入母亲手机"
            v-model="form.motherCellphone"
            label="母亲手机"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-form-input
            v-if="!data.disabled"
            field="remarks"
            layout="vertical"
            inputAlign="left"
            v-model="form.remarks"
            :height="160"
            label="备注"
            type="textarea"
            placeholder="请输入备注信息"
            showClear
            :transprent="true"
          ></zh-form-input>
          <zh-sheet
            :round="3"
            v-if="!data.disabled && props.myCheckInfo.editBaseFlag && !props.myCheckInfo.bmFlag && form.authStatus !== '0' && false"
            :margin="[0, 0]"
            :padding="[20, 20]"
            color="deep-orange"
            text
          >
            <zh-text label="您孩子的身份信息由孩子现在就读小学（幼儿园）录入，如果信息有误，请联系孩子所在学校。"></zh-text>
          </zh-sheet>
          <zh-form-upload
            v-if="!data.disabled"
            field="fileInfo"
            v-model="form.fileInfo"
            v-model:fileList="form.fileList"
            label="材料上传"
            required
            :defaultValue="[{ remarks: '户口本第一页' }, { remarks: '户口本户主页' }, { remarks: '户口本孩子页' }]"
            :fileType="['image']"
            :rows="3"
            :width="620"
            :rules="[{ required: true, message: '请上传户口本等照片，不少于3张' }]"
          >
            <template v-slot:desc>
              <zh-text color="grey-darken-2" :font-size="22" label="1.新生和父母在同一户口本，需上传①户主页，②父亲页， ③母亲页，④新生页；"></zh-text>
              <zh-text
                color="grey-darken-2"
                :font-size="22"
                label="2.新生和父母未在同一户口本：需上传①新生所在户主页，②父亲页，③母亲页，④新生页，⑤父母结婚证或孩子出生证；"
              ></zh-text>
              <zh-text color="grey-darken-2" :font-size="22" label="3.上传照片的信息需要完整、清晰，不可缺角。"></zh-text>
            </template>
          </zh-form-upload>
          <zh-form-item :border="false" v-if="!data.disabled">
            <view class="flex flex-row">
              <view class="flex-1 mr-32" style="margin: 0 auto">
                <zh-button form-type="submit" :disabled="data.btnDisabled" :loading="data.btnDisabled" label="提交信息" block color="#85a5ff" linear="right"></zh-button>
                <zh-button label="取消" block :followTheme="false" color="grey" @click="qxClick"></zh-button>
              </view>
            </view>
          </zh-form-item>
          <view v-if="form.authStatus !== '1'" style="padding-top: 40rpx">
            <view style="border-left: 10rpx solid #2f54eb">
              <zh-text :fontSize="36" class="text-weight-b pl-10" label="审核信息"></zh-text>
            </view>
            <zh-form-item :margin="[0, 0]" label="审核状态" field="authStatusName"
              ><view style="width: 100%"
                ><zh-tag :followTheme="false" :shadow="0" :color="form.authStatus === '2' ? 'red' : 'orange'" text size="n" :label="form.authStatusName"></zh-tag></view
            ></zh-form-item>
            <zh-form-input
              v-if="form.authStatus === '2'"
              field="authRemarks"
              layout="vertical"
              inputAlign="left"
              v-model="form.authRemarks"
              :height="160"
              label="审核意见"
              type="textarea"
              :transprent="true"
              placeholder="无"
            ></zh-form-input>
          </view>
          <zh-sheet
            :round="3"
            v-if="data.disabled && props.myCheckInfo.editBaseFlag && !props.myCheckInfo.bmFlag && !form.bmFlag"
            :margin="[0, 0]"
            :padding="[20, 20]"
            color="deep-orange"
            text
          >
            <zh-text v-if="form.authStatus !== '0'">如果新生基本信息有误，可<span style="color: #3b5cf0; font-weight: 600" @click="editClick">申请修改</span>。</zh-text>
            <zh-text v-if="form.authStatus === '0'" style=" padding-top: 10rpx" label="您的信息修改申请正在审核中，请耐心等待。"></zh-text>
          </zh-sheet>
        </view>
        <zh-sheet :margin="[0, 0]" :padding="[0, 20, 0, 0]">
          <view class="flex-row-center-center" @click="() => (form.expandFlag = !form.expandFlag)">
            <zh-text :label="form.expandFlag ? '收起' : '展开'" color="light-blue" style="padding-right: 10rpx"></zh-text>
            <tm-icon :fontSize="30" color="light-blue" :name="form.expandFlag ? 'tmicon-angle-up' : 'tmicon-angle-down'"></tm-icon>
          </view>
        </zh-sheet>
      </template>
    </zh-card>
  </zh-form>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted, defineProps, computed } from 'vue';
import * as util from '@/tool/function/util';
import { system } from '@/store';

// ------------------接收传入参数(组件)-------------------
const props = defineProps({
  myCheckInfo: {
    type: Object,
    default: () => {},
  },
  modelValue: {
    type: Object,
    default: () => {
      return {
        fullName: '',
        cardType: '01',
        enrollPreType: '2',
        motherCardType: '01',
        fatherCardType: '01',
        expandFlag: true,
      };
    },
  },
});
// ------------------钩子函数----------------------------
// 初始化
onMounted(() => {
  getAreaCode();
  enInfo();
});
const emits = defineEmits(['update:modelValue', 'showMessage']);
// ------------------变量区------------------------------
const formRef = ref();
// 主数据
let form = ref<any>(
  Object.assign(
    {
      fullName: '',
      cardType: '01',
      enrollPreType: '2',
      motherCardType: '01',
      fatherCardType: '01',
      expandFlag: true,
    },
    props.modelValue
  )
);
const content = ref([
  {
    icon: 'tmicon-alert',
    content: '根据当前信息，您不符合东阳义务段学校最低报名条件。',
  },
]);
const data = reactive({
  //脱敏
  hiddenFlag: true,
  //信息提交按钮
  btnDisabled: false,
  oldForm: Object.assign(
    {
      fullName: '',
      cardType: '01',
      enrollPreType: '2',
      motherCardType: '01',
      fatherCardType: '01',
      expandFlag: true,
    },
    props.modelValue
  ),
  sexDict: [
    {
      dictKey: '1',
      dictValue: '男',
    },
    {
      dictKey: '2',
      dictValue: '女',
    },
  ],
  cardTypeDict: [
    {
      dictKey: '01',
      dictValue: '身份证',
    },
    {
      dictKey: '99',
      dictValue: '其他证件',
    },
  ],
  areaCodeDict: [] as any,
  basicForm: {} as any,
  // 按钮信息
  btnInfo: {
    text: '一建报名',
    bgColor: 'linear-gradient(to right, #B9EDFA, #3DBAEA)',
  },
  // 可编辑标志
  disabled: true,
});

// ------------------方法区------------------------------
//获取可选择的行政区划
const getAreaCode = () => {
  $api.common.send
    .post('dict0028', {})
    .then((res: any) => {
      data.areaCodeDict = res.result || [];
    })
    .catch(() => {});
};
const changeSensitive = () => {
  data.hiddenFlag = !data.hiddenFlag;
  enInfo();
};
//点击编辑按钮
const editClick = () => {
  data.disabled = !data.disabled;
  if (!data.disabled) {
    form.value = Object.assign(form.value, data.oldForm);
    form.value.fileInfo=""
  }
};
//点击取消按钮
const qxClick = () => {
  formRef.value.clearValidate();
  formRef.value.reset();
  data.disabled = !data.disabled;
  enInfo();
};
// 提交
const submit = (e: any) => {
  if (e.validate) {
    if (form.value.fileList) {
      if (form.value.fileList.length < 3) {
        let messageData: any = {};
        messageData.model = 'warn';
        messageData.text = '请上传至少三张相关照片材料！';
        emits('showMessage', messageData);
        return;
      } else {
        let i = 0;
        form.value.fileList.forEach((item: any) => {
          if (item.showId) {
            i = i + 1;
          }
        });
        if (i < 3) {
          let messageData: any = {};
          messageData.model = 'warn';
          messageData.text = '请上传至少三张相关照片材料！';
          emits('showMessage', messageData);
          return;
        }
      }
    }

    if (form.value.cardType === '01' && !util.isIdCard(form.value.idcard)) {
      let messageData: any = {};
      messageData.model = 'warn';
      messageData.text = '请输入正确的学生身份证！';
      emits('showMessage', messageData);
      return;
    }
    let fimalyFlag = false;
    if (form.value.fatherIdcard) {
      if (form.value.fatherCardType === '01' && !util.isIdCard(form.value.fatherIdcard)) {
        let messageData: any = {};
        messageData.model = 'warn';
        messageData.text = '请输入正确的父亲（监护人）身份证！';
        emits('showMessage', messageData);
        return;
      }
      fimalyFlag = true;
    }
    if (form.value.fatherCellphone) {
      if (!util.isPhone(form.value.fatherCellphone)) {
        let messageData: any = {};
        messageData.model = 'warn';
        messageData.text = '请输入正确的父亲（监护人）手机号码！';
        emits('showMessage', messageData);
        return;
      }
    }
    if (form.value.motherCellphone) {
      if (!util.isPhone(form.value.motherCellphone)) {
        let messageData: any = {};
        messageData.model = 'warn';
        messageData.text = '请输入正确的母亲手机号码！';
        emits('showMessage', messageData);
        return;
      }
    }
    if (form.value.motherIdcard) {
      if (form.value.motherCardType === '01' && !util.isIdCard(form.value.motherIdcard)) {
        let messageData: any = {};
        messageData.model = 'warn';
        messageData.text = '请输入正确的母亲身份证！';
        emits('showMessage', messageData);
        return;
      }
      fimalyFlag = true;
    }
    if (!fimalyFlag) {
      let messageData: any = {};
      messageData.model = 'warn';
      messageData.text = '父亲（监护人）或母亲信息至少输入一方！';
      emits('showMessage', messageData);
      return;
    }
    //校验通过
    data.btnDisabled = true;
    $api.common.send
      .post('edustudentenrollpre0011', form.value, null, true)
      .then((res: any) => {
        form.value.authStatus = res.result.authStatus;
        form.value.authStatusName = res.result.authStatusName;
        data.oldForm = Object.assign({}, form.value);
        emits('update:modelValue', form.value);
        data.btnDisabled = false;
        data.disabled = !data.disabled;
        enInfo();
      })
      .catch((error: any) => {
        console.log(error);
        data.btnDisabled = false;
      });
  }
};

const CallPhone = (e: any) => {
  //拨打热线
  let zlbFlag = false;
  // #ifdef H5
  zlbFlag = system.zlbFlag;

  //#endif
  if (zlbFlag) {
    if (ZWJSBridge) {
      ZWJSBridge.phoneCall({
        corpId: e,
      })
        .then((result: any) => {
          console.log(result);
        })
        .catch((error: any) => {
          console.log(error);
        });
    } else {
      uni.makePhoneCall({
        phoneNumber: e,
      });
    }
  } else {
    uni.makePhoneCall({
      phoneNumber: e,
    });
  }
};

const gotoYjbm = () => {
  //前往一键报名页面
  if (!form.value.bmFlag) {
    $router.push('/pages/edu/enroll/yjbmList', { studentEnrollPreId: form.value.studentEnrollPreId });
  } else {
    $router.replace('/pages/edu/enroll/myEnroll', {});
  }
};
const enInfo = () => {
  if (data.hiddenFlag) {
    form.value.fullName = getEnName(form.value.fullName);
    form.value.idcard = getEnIdcard(form.value.idcard);
    form.value.fatherName = getEnName(form.value.fatherName);
    form.value.fatherIdcard = getEnIdcard(form.value.fatherIdcard);
    form.value.fatherCellphone = getEnCellphone(form.value.fatherCellphone);
    form.value.motherName = getEnName(form.value.motherName);
    form.value.motherIdcard = getEnIdcard(form.value.motherIdcard);
    form.value.motherCellphone = getEnCellphone(form.value.motherCellphone);
  } else {
    form.value.fullName = data.oldForm.fullName;
    form.value.idcard = data.oldForm.idcard;
    form.value.fatherName = data.oldForm.fatherName;
    form.value.fatherIdcard = data.oldForm.fatherIdcard;
    form.value.fatherCellphone = data.oldForm.fatherCellphone;
    form.value.motherName = data.oldForm.motherName;
    form.value.motherIdcard = data.oldForm.motherIdcard;
    form.value.motherCellphone = data.oldForm.motherCellphone;
  }
};
//姓名加密
const getEnName = (fullName: any) => {
  if (!fullName) {
    return '';
  }
  return '*' + fullName.substring(1, fullName.length);
};
//身份证加密
const getEnIdcard = (idCard: any) => {
  if (!idCard) {
    return '';
  }
  let enCard = '';
  if (idCard.length === 18) {
    enCard = idCard.replace(/(\w{1})\w*(\w{1})/, '$1****************$2');
  } else {
    let len = idCard.length / 3;
    let baseLen = idCard.length - len * 2;
    enCard = idCard.substring(0, len);

    for (var i = 0; i < baseLen; i++) {
      enCard = enCard + '*';
    }
    enCard = enCard + idCard.substring(len * 2, idCard.length);
  }
  return enCard;
};
//手机号码加密
const getEnCellphone = (cellphone: any) => {
  if (!cellphone) {
    return '';
  }
  let enCellphone = '';
  if (cellphone.length === 11) {
    enCellphone = cellphone.replace(/(\w{3})\w*(\w{4})/, '$1****$2');
  } else {
    let len = cellphone.length / 3;
    let baseLen = cellphone.length - len * 2;
    enCellphone = cellphone.substring(0, len);

    for (var i = 0; i < baseLen; i++) {
      enCellphone = enCellphone + '*';
    }
    enCellphone = enCellphone + cellphone.substring(len * 2, cellphone.length);
  }
  return enCellphone;
};
</script>
<style lang="scss" scoped></style>
